package com.zlx.module_base.base_util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.text.TextUtils;

import java.lang.reflect.Method;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUt {
    /**
     * 判断一个字符串是否为空，或null，或"null"
     *
     * @param str 字符串
     * @return
     */
    public static boolean isEmpty(String str) {
        return TextUtils.isEmpty(str) || "null".equals(str);
    }

    /**
     * 判断一个字符串是否为非空字符串
     *
     * @param str 字符串
     * @return
     */
    public static boolean notEmpty(String str) {
        return !isEmpty(str);
    }

    /**
     * 字符串去空格、换行符等
     *
     * @param str
     * @return
     */
    public static String replaceBlank(String str) {
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }

    /**
     * 判断身份证号码是否正确
     *
     * @param idCardNo 身份证号码
     * @return
     */
    public static boolean isIDCardNo(String idCardNo) {
        if (isEmpty(idCardNo)) {
            return false;
        }
        /** 身份证号码正则表达式 */
        String regex = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(idCardNo);
        return matcher.matches();
    }

    /**
     * 判断车牌号是否正确
     *
     * @param carNo 车牌号码
     * @return
     */
    public static boolean isCarNo(String carNo) {
        if (isEmpty(carNo)) {
            return false;
        }
        /** 车牌号格式：汉字 + A-Z + 5位A-Z或0-9 （只包括了普通车牌号，教练车和部分部队车等车牌号不包括在内）*/
        // String regex = "[\u4e00-\u9fa5]{1}[A-Z]{1}[A-Z_0-9]{5}";
        String regex = "^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[警京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]{0,1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(carNo);
        return matcher.matches();
    }

    /**
     * 判断是否为手机号码,验证第一位是1，后面10个数字
     *
     * @param phoneNo 手机号
     * @return
     */
    public static boolean isPhoneNo(String phoneNo) {
        if (isEmpty(phoneNo)) {
            return false;
        }
        String expression = "^1[0-9]{10}$";
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(phoneNo);
        return matcher.matches();
    }

    /**
     * 将手机号格式化，中间4位隐藏
     *
     * @param phoneNo 手机号码
     * @return
     */
    public static String formatPhoneNo(String phoneNo) {
        if (!isPhoneNo(phoneNo)) {
            return phoneNo;
        }
        return phoneNo.substring(0, 3) + "****" + phoneNo.substring(7);
    }

    /**
     * 判断是否为邮箱
     *
     * @param email 邮箱
     * @return
     */
    public static boolean isEmail(String email) {
        if (isEmpty(email)) {
            return false;
        }
        String expression = "^[a-zA-Z0-9]+([\\_|\\-|\\.]?[a-zA-Z0-9])*\\@[a-zA-Z0-9]+([\\_|\\-|\\.]?[a-zA-Z0-9])*\\.[a-zA-Z]{2,3}$";
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }

    /**
     * 判断密码格式是否正确,6~16位字母及数字组合
     *
     * @param password 密码字符串
     * @return
     */
    public static boolean isPsssword(String password) {
        if (isEmpty(password)) {
            return false;
        }
        String expression = "^[a-zA-Z0-9]{6,16}$";
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(password);
        return matcher.matches();
    }

    /**
     * 校验银行卡卡号
     *
     * @param cardId 银行卡卡号
     * @return
     */
    public static boolean checkBankCard(String cardId) {
        if (isEmpty(cardId)) {
            return false;
        }
        char bit = getBankCardCheckCode(cardId.substring(0, cardId.length() - 1));
        if (bit == 'N') {
            return false;
        }
        return cardId.charAt(cardId.length() - 1) == bit;
    }

    /**
     * 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位
     *
     * @param nonCheckCodeCardId 不含校验位的银行卡卡号
     * @return
     */
    private static char getBankCardCheckCode(String nonCheckCodeCardId) {
        if (isEmpty(nonCheckCodeCardId) || !nonCheckCodeCardId.matches("\\d+")) {
            // 如果传的不是数字返回N
            return 'N';
        }
        char[] chs = nonCheckCodeCardId.trim().toCharArray();
        int luhmSum = 0;
        for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
            int k = chs[i] - '0';
            if (j % 2 == 0) {
                k *= 2;
                k = k / 10 + k % 10;
            }
            luhmSum += k;
        }
        return (luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0');
    }

    /**
     * 生成指定长度的随机字符串
     *
     * @param length 字符串长度
     * @return
     */
    public static String genRandom(int length) {
        // length表示生成字符串的长度
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 获取版本号
     *
     * @return 当前应用的版本号
     */
    public static String getVersion(Context ctx) {
        try {
            PackageManager manager = ctx.getPackageManager();
            PackageInfo info = manager.getPackageInfo(ctx.getPackageName(), 0);
            String version = info.versionName;
            return "v" + version;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 获取版本号
     *
     * @return 当前应用的版本号
     */
    public static int getVersionCode(Context ctx) {
        PackageManager manager = ctx.getPackageManager();
        PackageInfo info = null;
        try {
            info = manager.getPackageInfo(ctx.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        int version = info.versionCode;
        return version;
    }

    /**
     * 通过名字生成一个颜色值
     *
     * @param name
     * @return
     */
    public static int nameToColor(String name, int r, int g, int b) {
        String[] strArray = toUnicode(name).replace("\\", "").split("u");
        if (strArray.length > 1) {
            try {
                b = Integer.parseInt(strArray[1].length() > 6 ? strArray[1].substring(0, 6) : strArray[1], 16);
                b = b % 255;
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        if (strArray.length > 2) {
            try {
                g = Integer.parseInt(strArray[2].length() > 6 ? strArray[2].substring(0, 6) : strArray[2], 16);
                g = g % 255;
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        if (strArray.length > 3) {
            try {
                r = Integer.parseInt(strArray[3].length() > 6 ? strArray[3].substring(0, 6) : strArray[3], 16);
                r = r % 255;
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }

        }
        return Color.rgb(r, g, b);
    }

    /**
     * 字符串转为Unicode码
     *
     * @param str
     * @return
     */
    public static String toUnicode(String str) {
        char[] arChar = str.toCharArray();
        int iValue = 0;
        String uStr = "";
        for (int i = 0; i < arChar.length; i++) {
            iValue = (int) str.charAt(i);
            if (iValue <= 256) {
                uStr += "\\" + Integer.toHexString(iValue);
            } else {
                uStr += "\\u" + Integer.toHexString(iValue);
            }
        }
        return uStr;
    }

    /**
     * 传入全类名获得对应类中所有方法名和参数名
     */
    public static void getMethodInfo(String pkgName) {
        try {
            Class clazz = Class.forName(pkgName);
            Method[] methods = clazz.getMethods();
            for (Method method : methods) {
                String methodName = method.getName();
                System.out.println("方法名称:" + methodName);
                Class<?>[] parameterTypes = method.getParameterTypes();
                for (Class<?> clas : parameterTypes) {
                    String parameterName = clas.getName();
                    System.out.println("参数名称:" + parameterName);
                }
                System.out.println("*****************************");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}